const router = require('koa-router')()
const sqlQuery = require('../db')
const multer = require('koa-multer');
const path = require('path');
//设置上传
let storage = multer.diskStorage({
  destination: path.resolve('public/videos'),
  filename: (ctx, file, cb)=>{
    let suffix = file.originalname.split('.')[1];
      cb(null, Date.now()+"."+suffix);
  }
});
let fileFilter = (ctx, file ,cb)=>{
//过滤上传的后缀为txt的文件
    let suffix = file.originalname.split('.')[1];
    // AVI、mov、rmvb、rm、FLV、mp4、3GP
  if (!suffix in ['AVI','mov','rmvb','rm','FLV','mp4','3GP']){
      cb(null, false); 
  }else {
      cb(null, true); 
  }
}
let upload = multer({ storage: storage, fileFilter: fileFilter });

//获取文件上传post表单参数
router.post('/uploadVideo', upload.single('upvideo'), async ctx => {
    let { userid, title, classify } = ctx.req.body;
    let url = 'http://localhost:3000/videos/'+ctx.req.file.filename;
    // console.log(ctx.req.body.userid, ctx.req.file.filename, ctx.req.body.title, ctx.req.body.classify);
    let sql = `INSERT INTO happy_video(title,url,class_id,user_id) VALUES(?,?,?,?)`;
    let result = await sqlQuery(sql, [title, url, classify, userid]);
    ctx.body = {
        state: 200,
        message:result
    }
    // let { bookId, bookName, pubId, createTime } = ctx.req.body;
    // let pic = ctx.req.body.picold;
  
    // if (ctx.req.file !== undefined) {
    //   pic = ctx.req.file.filename;
    // }
    // let sql = "update books set bookName=?,createTime=?,pubId=?,pic=? where bookId=?";
    // let pms = [bookName, createTime, pubId, pic, bookId];
    // try {
    //   await sqlQuery(sql, pms);
    //   ctx.redirect('/bs/');
    // } catch (error) {
    //   ctx.render('error.html', { err: error });
    // }
});

//左侧菜单数据
router.get("/firstMenu", async (ctx, next) => {
    try {
        let array = []
        let sql = "select name from leftmenu where did=1";
        let result = await sqlQuery(sql, []);
        result.map(item => {
            array.push(item.name);
        })
        ctx.body = {
            status: 1,
            message: array
        }
  
    } catch (error) {
      next(error);
    }
});
//右侧内容数据
router.get("/secondMenu/:id", async (ctx, next) => {
    try {
        let array = [];
        let { id } = ctx.params;
        let sql = "select r.name from leftmenu l,rightcontent r where l.id=? and l.id=r.pid";
        let result = await sqlQuery(sql, [id]);
        result.map(item => {
            array.push(item.name);
        })
        ctx.body = {
            status: 1,
            message: array
        }
  
    } catch (error) {
      next(error);
    }
});
//  获取视频id
router.get("/getClassIdByName/:name", async (ctx, next) => {
    try {
        let { name } = ctx.params;
        let sql = "SELECT id FROM rightcontent WHERE name=?";
        let result = await sqlQuery(sql, [name]);
        ctx.body = {
            status: 1,
            message: result[0].id
        }
  
    } catch (error) {
      next(error);
    }
});
module.exports = router